"ui";
//2019.5.18 8.19
ui.layout(
    <vertical>
        <horizontal>
            <img id="exit" src="file://./res/ic_keyboard_backspace_black_48dp.png" w="40" h="40" gravity="center"/>
            <text text="支付宝语音到账合成"  textColor="#333333" textSize="27sp" typeface="serif" gravity="top|center" w="*" textStyle="bold"/>
        </horizontal>
        <vertical padding="16">
            <text textSize="18sp" textColor="black" text="在下面的框框输入内容"/>
            <input id="input" digit=".0123456789" singleLine="true" hint="在这里输入金额："/>
            <button id="ok" text="确定"/>
        </vertical>
    </vertical>
);
//如果数字含有小数部分，那么可以将小数部分单独取出
//将小数部分的数字转换为字符串的方法：
var chnNumChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
var chnUnitSection = ["", "万", "亿", "万亿", "亿亿"];
var chnUnitChar = ["", "十", "百", "千"];

ui.exit.click(() => {
    ui.finish();
});

ui.ok.on("click", () => {
    let num = ui.input.text();
    if (num.length != 0) {
        threads.start(function() {
            let is = ZFB_TTS(num);
            if (is) {
                dialogs.confirm("转换完成", "现在要不要打开" + is + "文件", (vale) => {
                    if (vale) {
                        app.viewFile(is);
                    }
                });
            } else {
                return toast("转换失败");
            }
        });
    }
});


function ZFB_TTS(money) {
    var arr = [];
    let path = files.cwd();
    let mo = TransformToChinese(money);
    let my = mo.toString().split("");
    arr.push(path + "/tts/支付宝到账.mp3");
    for (let i = 0; i < my.length; i++) {
        arr.push(path +"/tts/" + my[i] + ".mp3");
    }
    arr.push(path + "/tts/元.mp3");
    let mp3path = "/sdcard/支付宝到账" + money + "元.mp3";
    try {
        uniteAMRFile(arr, mp3path);
        return mp3path;
    } catch (e) {
        toast(e);
        return false;
    }
}

//定义在每个小节的内部进行转化的方法，其他部分则与小节内部转化方法相同
function sectionToChinese(section) {
    var str = '',
        chnstr = '',
        zero = false,
        count = 0; //zero为是否进行补零， 第一次进行取余由于为个位数，默认不补零
    while (section > 0) {
        var v = section % 10; //对数字取余10，得到的数即为个位数
        if (v == 0) { //如果数字为零，则对字符串进行补零
            if (zero) {
                zero = false; //如果遇到连续多次取余都是0，那么只需补一个零即可
                chnstr = chnNumChar[v] + chnstr;
            }
        } else {
            zero = true; //第一次取余之后，如果再次取余为零，则需要补零
            str = chnNumChar[v];
            str += chnUnitChar[count];
            chnstr = str + chnstr;
        }
        count++;
        section = Math.floor(section / 10);
    }
    return chnstr;
}


function TransformToChinese(num) {
    var a = (numToChn(num) == false) ? '' : numToChn(num);
    num = Math.floor(num);
    var unitPos = 0;
    var strIns = '',
        chnStr = '';
    var needZero = false;

    if (num === 0) {
        return chnNumChar[0];
    }
    while (num > 0) {
        var section = num % 10000;
        if (needZero) {
            chnStr = chnNumChar[0] + chnStr;
        }
        strIns = sectionToChinese(section);
        strIns += (section !== 0) ? chnUnitSection[unitPos] : chnUnitSection[0];
        chnStr = strIns + chnStr;
        needZero = (section < 1000) && (section > 0);
        num = Math.floor(num / 10000);
        unitPos++;
    }
    log(chnStr + a);
    return chnStr + a;
}


/*
partsPaths  合成前的文件
pathe 合成后的路径

*/
function uniteAMRFile(partsPaths, unitedFilePath) {
    try {
        var unitedFile = new java.io.File(unitedFilePath);
        var fos = new java.io.FileOutputStream(unitedFile);
        var ra = null;
        for (var i = 0; i < partsPaths.length; i++) {
            ra = new java.io.RandomAccessFile(partsPaths[i], "r");
            if (i != 0) {
                ra.seek(6);
            }
            var buffer = java.lang.String(new Array(1024 * 8 + 1).toString()).getBytes();
            var len = 0;
            while ((len = ra.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
        }
        ra.close();
        fos.close();
    } catch (e) {}
}
//定义整个数字全部转换的方法，需要依次对数字进行10000为单位的取余，然后分成小节，按小节计算，当每个小节的数不足1000时，则需要进行补零
function numToChn(num) {
    var index = num.toString().indexOf(".");
    if (index != -1) {
        var str = num.toString().slice(index);
        var a = "点";
        for (var i = 1; i < str.length; i++) {
            a += chnNumChar[parseInt(str[i])];
        }
        return a;
    } else {
        return !1;
    }
}